package leetcode;

public class Problem28 {
    public static void main(String[] args) {
        Problem28 pp = new Problem28();
        System.out.println(pp.strStr("mississippi", "issip"));
    }

    public int strStr(String haystack, String needle) {
        int lenA = haystack.length();
        int lenB = needle.length();
        if (lenB == 0) {
            return 0;
        }
        if (lenA == 0) {
            return -1;
        }
        int pA = 0;
        int pB = 0;
        while (pA < lenA) {
            if (haystack.charAt(pA) == needle.charAt(pB)) {
                if (pB == lenB - 1) {
                    return pA - lenB + 1;
                }
                pB++;
                pA++;
            } else {
                pA = pA - lenB + 1;
                pB = 0;
            }
        }
        return -1;
    }
}
